<?php
namespace Wanhu\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function login(){
        $this->display();
    }

    public function index(){
        $this->isLogin();
        $PrizeModel = D('Prize');
        $list = $PrizeModel->getPrizeList();
        $this->assign('list', $list);
        $this->display();
    }

    public function changePrizeNum(){
        $this->isLogin();
        $prizeModel = D('Prize');
        $id = I('post.prize_id');
        $num = I('post.prize_num');
        if($id===null || $num===null){
            $msg = array(
                'err'   => 1,
                'title' => '表单不完整',
                'info' => '表单不完整,请填写完整再试',
            );
            $this->assign('msg',$msg);
            $this->display('info');
            return false;
        }
        $result = $prizeModel->setPrizeNum($id, $num);
        if($result===0){
            $msg = array(
                'err'   => 2,
                'title' => '未发生改变……',
                'info' => '不知道为什么,好像什么变化都没发生',
            );
            $this->assign('msg',$msg);
            $this->display('info');
            return false;
        }
        if($result===false){
            $msg = array(
                'err'   => 3,
                'title' => '修改失败！',
                'info' => '似乎是有点麻烦的状况，请迅速联系技术人员',
            );
            $this->assign('msg',$msg);
            $this->display('info');
            return false;
        }
        $info = $prizeModel->getIdPrize($id);
        $msg = array(
            'err'   => 0,
            'title' => '修改成功',
            'info' => $info['name']."的总量目前是:".$info['num']
        );
        $this->assign('msg',$msg);
        $this->display('info');
        return;
    }

    private function isLogin(){
        if(session('isLogin')==1)
            return true;
        $username = I('post.username');
        $passwd = I('post.passwd');
        if(empty($username) || empty($passwd))
            $this->redirect('login');
        $access = array(
            'misomed' => 'misomed1218',
        );
        if($access[$username] != $passwd)
            $this->redirect('login');
        session('isLogin', 1);
        return true;
    }
}
